<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>数据集</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui/css/layui.css">
    <link rel="stylesheet" href="../css/scroll-bar.css">
    <link rel="stylesheet" href="../css/sub-page.css">
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../lib/nprogress/nprogress.css">
</head>

<body>
    <div class="ok-body">
        <!--面包屑导航区域-->
        <div class="ok-body-breadcrumb">
            <span class="layui-breadcrumb">
                <a><cite>首页</cite></a>
                <a><cite>数据集管理</cite></a>
                <a><cite>数据集</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" href="javascript:location.replace(location.href);" title="刷新">
                <i class="layui-icon layui-icon-refresh"></i>
            </a>
        </div>
        <!--模糊搜索区域-->
        <div class="layui-row ok-search">
            <input class="layui-input" placeholder="开始日期" autocomplete="off" id="startTime">
            <input class="layui-input" placeholder="截止日期" autocomplete="off" id="endTime">
            <div class="layui-inline">
                <input class="layui-input" name="id" id="dsname" autocomplete="off" placeholder="请输入数据集名称">
            </div>
            <div class="layui-inline">
                <input class="layui-input" name="id" id="dstags" autocomplete="off" placeholder="数据集标签">
            </div>

            <button class="layui-btn" data-type="reload" id="search">搜索</button>
        </div>
        <!--工具栏-->
        <okToolbar>
            <!--   <button class="layui-btn layui-btn-normal" id="batchEnabled">
                <i class="iconfont icon-shangsheng"></i>批量启用
            </button>
            <button class="layui-btn layui-btn-warm" id="batchDisabled">
                <i class="iconfont icon-web-icon-"></i>批量停用
            </button>
            <button class="layui-btn layui-btn-danger" id="batchDel">
                <i class="layui-icon layui-icon-delete"></i>批量删除
            </button> -->
            <button class="layui-btn" id="add">
                <i class="layui-icon">&#xe61f;</i>新建数据集
            </button>
            <span>共有数据：<i id="countNum">0</i> 条</span>
        </okToolbar>
        <!--数据表格-->
        <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
    </div>
    <!--js逻辑-->
    <script src="../lib/layui/layui.js"></script>
    <script src="../lib/nprogress/nprogress.js"></script>
    <script>
        NProgress.start();
    window.onload = function () {
        NProgress.done();
    }
    layui.use(['element', 'table', 'form', 'jquery','laydate'], function () {
        var element = layui.element;
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#startTime',
            type: "datetime"
        });

        laydate.render({
            elem: '#endTime',
            type: "datetime"
        });
        table.render({
            elem: '#tableId',
            url: '/dataset/list',
            limit: 10,
            cellMinWidth: 80,
            page: true,
            id:'folderlist',
            cols: [[               
                {field: 'dsid', title: 'ID', width: 80, sort: true},
                {field: 'dsname', title: '数据集',edit: 'text'},
                {field: 'totalcount', title: '数据总量',width:90},
                {field: 'dstags', title: '数据集标签',edit: 'text'},
                {field: 'dstype', title: '数据源',templet: '#typeTpl'},
                {field: 'description', title: '描述',width:120,edit: 'text'},
                {field: 'createat', title: '创建时间'},
                {field: 'status', title: '状态', width: 95, templet: '#statusTpl'},
                {field: 'actions',title: '类型',templet: '#actionTpl'},
                {field: 'createtype', title: '创建方式',width:95,  templet: '#createtypeTpl'},
                {title: '操作', templet: '#operationTpl', align: 'center'}
            ]],
            done: function (res, curr, count) {
                $("#countNum").text(count);
            }
        });

        var active = {
            reload: function(){
                var dsname = $('#dsname').val();
                var dstags = $('#dstags').val();
                var start=$('#startTime').val();
                var end=$('#endTime').val();
                
                //执行重载
                table.reload('folderlist', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        key:{
                            dsname:dsname,
                            dstags:dstags,
                            start:start,
                            end:end                           
                        }
                    }
                });
            }
        };

        $('#search').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('#dsname,#dstags').on('keydown',function(e){
            if(e.keyCode=='13'){
                $('#search').click();
            }
        });

        table.on('tool(tableFilter)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'dsbind') {
                layer.open({
                    title: '数据源',
                    type: 2,
                    shade: false,
                    maxmin: true,
                    shade: 0.5,
                    area: ['90%', '90%'],
                    content: '/dataset/dsbind',
                    zIndex: layer.zIndex,
                    end: function () {
                        $(".layui-laypage-btn")[0].click();
                    },
                    success:function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        //console.log(body.html()) //得到iframe页的body内容
                        //console.log(body.find('#guestid').val());
                        body.find('#id').val(data.dsid);
                        console.log(body.find('#id').val());
                    }
                });
            } else if (layEvent === 'fsbind') {
                layer.open({
                    title: '文件源',
                    type: 2,
                    shade: false,
                    maxmin: true,
                    shade: 0.5,
                    area: ['90%', '90%'],
                    content: '/dataset/fsbind',
                    zIndex: layer.zIndex,
                    end: function () {
                        $(".layui-laypage-btn")[0].click();
                    },
                    success:function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        body.find('#id').val(data.dsid);
                        console.log(body.find('#id').val());
                    }
                });
            }else if (layEvent === 'finish') {
                layer.confirm("确认创建？", {
                    skin: 'layui-layer-lan',
                    icon: 3,
                    title: '提示',
                    anim: 6
                }, function () {
                    console.log(data);
                    $.ajax({
                    url: '/dataset/finish',
                    method: 'post',
                    data: {dsid:data.dsid},
                    dataType: 'JSON',
                    success: function (res) {
                            if (res.code == 1) {
                                $('#search').click();
                                layer.closeAll();  // 关闭layer
                            } else {
                                layer.msg('操作失败');
                            }
                        },
                        error: function (data) {}
                    }) 
                });
            }else if (layEvent === 'publish') {
                layer.confirm("确认发布？", {
                    skin: 'layui-layer-lan',
                    icon: 3,
                    title: '提示',
                    anim: 6
                }, function () {
                    console.log(data);
                    $.ajax({
                    url: '/dataset/publish',
                    method: 'post',
                    data: {dsid:data.dsid,dstype:data.dstype},
                    dataType: 'JSON',
                    success: function (res) {
                            if (res.code == 1) {
                                $('#search').click();
                                layer.closeAll();  // 关闭layer
                            } else {
                                layer.msg('操作失败');
                            }
                        },
                        error: function (data) {}
                    }) 
                });
            }else if (layEvent === 'detail') {
                layer.open({
                    title: '当前数据源',
                    type: 2,
                    shade: false,
                    maxmin: true,
                    shade: 0.5,
                    area: ['90%', '90%'],
                    content: '/dataset/resourcelist',
                    zIndex: layer.zIndex,
                    end: function () {
                        $(".layui-laypage-btn")[0].click();
                    },
                    success:function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        body.find('#id').val(data.dsid);
                        console.log(body.find('#id').val());
                    }
                });
            }else if (layEvent === 'finish') {
                layer.confirm("确认创建？", {
                    skin: 'layui-layer-lan',
                    icon: 3,
                    title: '提示',
                    anim: 6
                }, function () {
                    console.log(data);
                    $.ajax({
                    url: '/dataset/finish',
                    method: 'post',
                    data: {dsid:data.dsid},
                    dataType: 'JSON',
                    success: function (res) {
                            if (res.code == 1) {
                                $('#search').click();
                                layer.closeAll();  // 关闭layer
                            } else {
                                layer.msg('操作失败');
                            }
                        },
                        error: function (data) {}
                    }) 
                });
            }else if (layEvent === 'delete') {
                layer.confirm("确认删除？", {
                    skin: 'layui-layer-lan',
                    icon: 3,
                    title: '提示',
                    anim: 6
                }, function () {
                    console.log(data);
                    $.ajax({
                    url: '/dataset/delete',
                    method: 'post',
                    data: {dsid:data.dsid},
                    dataType: 'JSON',
                    success: function (res) {
                            if (res.code == 1) {
                                $('#search').click();
                                layer.closeAll();  // 关闭layer
                            } else {
                                layer.msg('操作失败');
                            }
                        },
                        error: function (data) {}
                    }) 
                });
            }
        });


         $("#add").click(function () {
            layer.open({
                title: '新建任务',
                type: 2,
                shade: false,
                maxmin: true,
                shade: 0.5,
                anim: 4,
                area: ['90%', '90%'],
                content: '/dataset/add',
                zIndex: layer.zIndex,
                // skin: 'layui-layer-molv',
                end: function () {
                    $(".layui-laypage-btn")[0].click();
                }
            });
        });

        //监听单元格编辑
        table.on('edit(tableFilter)', function(obj){
            layer.confirm("确认修改？", {
                    skin: 'layui-layer-lan',
                    icon: 3,
                    title: '提示',
                    anim: 6
                }, function () {
                    var value = obj.value //得到修改后的值
                    ,data = obj.data //得到所在行所有键值
                    ,field = obj.field; //得到字段
                    //layer.msg('[ID: '+ data.dsid +'] ' + field + ' 字段更改为：'+ value);
                    if(value.trim('')==''){
                        layer.msg('数据项不能为空');
                        $('#search').click();
                        return;
                    }
                    var params= {dsid:data.dsid};
                    if(field=='dsname'){
                        params.dsname=value;
                        
                    }else if(field=='dstags'){
                        params.dstags=value;
                    }else if(field=='description'){
                        params.description=value;
                    }
                    $.ajax({
                            url: '/dataset/update',
                            method: 'post',
                            data:params,
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == 1) {
                                    $('#search').click();
                                    layer.closeAll();
                                } else {
                                    layer.msg('操作失败');
                                }
                            },
                            error: function (data) {}
                        })
                },function(){
                    $('#search').click();
                });

           
        });
    })
</script>
    <!--模板-->
    <script type="text/html" id="statusTpl">
        <a href="javascript:;" title="状态" class="layui-btn layui-btn-xs  {{ d.status== 0? 'layui-bg-orange' : 'layui-bg-green'}} ">
              {{#  if(d.status == 0){ }}
                未完成
              {{#  } }}
              {{#  if(d.status == 1){ }}
              创建完成
              {{#  } }}
              {{#  if(d.status == 2){ }}
                已发布
              {{#  } }}
        </a>
   
</script>

    <script type="text/html" id="typeTpl">
        <a href="javascript:;" title="数据源" lay-event="{{ d.dstype== -1 ? '' : 'detail'}}"  class="layui-btn layui-btn-xs {{ d.dstype== -1 ? 'layui-bg-red' : 'layui-bg-green'}}">
        {{#  if(d.dstype == 0){ }}
            数据集
          {{#  } }}
          {{#  if(d.dstype == 1){ }}
            文件系统
          {{#  } }}
          {{#  if(d.dstype == -1){ }}
            源未设置
          {{#  } }}
    </a>
</script>

    <script type="text/html" id="createtypeTpl">
        <a href="javascript:;" title="数据源" class="layui-btn layui-btn-xs {{ d.createtype== 1 ? 'layui-bg-green' : 'layui-bg-black'}}">{{ d.createtype== 1 ? '用户创建' : '系统生成'}}
    </a>
</script>
    <script type="text/html" id="operationTpl">
        {{#  if(d.status == 0){ }} <!-- 未完成状态 则允许绑定数据源-->
            {{#  if(d.dstype == 0){ }}<!-- dstype 1=文件夹  0=数据源-->
                {{#  if(d.createtype == 1){ }}
                <a href="javascript:;" title="数据集选择" lay-event="dsbind" class="layui-btn layui-btn-xs layui-bg-blue">数据集</a>
                <a href="javascript:;" title="标记为完成" lay-event="finish" class="layui-btn layui-btn-xs layui-bg-cyan">确认</a>
                {{#  } }}
            {{#  } }}

            {{#  if(d.dstype == 1){ }}<!-- dstype 1=文件夹  0=数据源-->
                {{#  if(d.createtype == 1){ }}
                <a href="javascript:;" title="文件夹选择" lay-event="fsbind" class="layui-btn layui-btn-xs layui-bg-blue">文件夹</a>
                <a href="javascript:;" title="标记为完成" lay-event="finish" class="layui-btn layui-btn-xs layui-bg-cyan">确认</a>
                {{#  } }}
            {{#  } }}
        
            {{#  if(d.dstype == -1){ }}<!-- dstype=-1  源未指定 -->
            <a href="javascript:;" title="数据集选择" lay-event="dsbind" class="layui-btn layui-btn-xs layui-bg-blue">数据集</a>
            <a href="javascript:;" title="文件夹选择" lay-event="fsbind" class="layui-btn layui-btn-xs layui-bg-blue">文件夹</a>
            {{#  } }}
            
            {{#  if(d.createtype == 1){ }}  <!-- 由任务创建而生成的数据集不允许被删除 -->
            <a href="javascript:;" title="删除数据集" lay-event="delete" class="layui-btn layui-btn-xs layui-bg-red">删除</a>
            {{#  } }}
      {{#  } }}

      {{#  if(d.status == 1){ }} <!-- 已完成 -->
         <a href="javascript:;" title="发布数据集" lay-event="publish" class="layui-btn layui-btn-xs layui-bg-blue">发布</a>
      {{#  } }}
            
     
       
        <!--<a href="javascript:;" title="文件列表" lay-event="list" class="layui-btn layui-btn-xs layui-bg-blue">设置</a>
         <a href="javascript:;" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a> -->
</script>

    <script type="text/html" id="actionTpl">
        <a href="javascript:;" title="执行类别"  class="layui-btn layui-btn-xs">
    {{#  if(d.actions === 0){ }}
    数据筛选
    {{#  } }}     
   {{#  if(d.actions === 1){ }}
    单分类
    {{#  } }}
    {{#  if(d.actions === 2){ }}
    多分类
    {{#  } }}  
    {{#  if(d.actions === -1){ }}
    未知
    {{#  } }}  
</a>

</body>

</html>